package com.stox.client.ui.javafx.core.util;

import javafx.css.Styleable;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

import com.stox.shared.i18n.MessageSource;
import com.stox.shared.util.StringUtil;

public class UI {

	public static <T extends Styleable> T style(final T node, final String... style) {
		node.getStyleClass().addAll(style);
		return node;
	}

	public static String web(final Color color) {
		return String.format("#%02X%02X%02X", (int) (color.getRed() * 255), (int) (color.getGreen() * 255), (int) (color.getBlue() * 255));
	}

	public static <T extends Control> T control(final T control, final String tooltip, final boolean isUndecorated, final boolean isGraphicOnly, final String size, final String... styles) {
		if (StringUtil.hasText(tooltip)) {
			control.setTooltip(new Tooltip(tooltip));
		}
		if (isUndecorated) {
			style(control, CSS.undecorated);
		}
		if (isGraphicOnly) {
			style(control, CSS.graphic);
		}
		style(control, size);
		return style(control, styles);
	}

	public static Button mediumCrossButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.crossButton);
	}

	public static Button mediumMinButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.minButton);
	}

	public static Button mediumMaxButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.maxButton);
	}

	public static Button mediumCheckButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.checkButton);
	}

	public static Button mediumGearButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.gearButton);
	}

	public static Button mediumPlusButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.plusButton);
	}

	public static Button mediumMinusButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.minusButton);
	}

	public static ToggleButton mediumMouseCursorButton(final String tooltipText) {
		return control(new ToggleButton(), tooltipText, true, true, CSS.mediumButton, CSS.mouseCursor);
	}

	public static Button mediumEditButton(final String tooltipText) {
		return control(new Button(), tooltipText, true, true, CSS.mediumButton, CSS.editButton);
	}

	/* Watchlist TODO should be in watchlist application */

	public static ToggleButton horizontalLineButton() {
		return control(new ToggleButton(), MessageSource.get("Horizontal.Line"), true, true, CSS.mediumButton, CSS.horizontalLine);
	}

	public static ToggleButton horizontalRayButton() {
		return control(new ToggleButton(), MessageSource.get("Horizontal.Ray"), true, true, CSS.mediumButton, CSS.horizontalRay);
	}

	public static ToggleButton horizontalSegmentButton() {
		return control(new ToggleButton(), MessageSource.get("Horizontal.Segment"), true, true, CSS.mediumButton, CSS.horizontalSegment);
	}

	public static ToggleButton verticalLineButton() {
		return control(new ToggleButton(), MessageSource.get("Vertical.Line"), true, true, CSS.mediumButton, CSS.verticalLine);
	}

	public static ToggleButton verticalRayButton() {
		return control(new ToggleButton(), MessageSource.get("Vertical.Ray"), true, true, CSS.mediumButton, CSS.verticalRay);
	}

	public static ToggleButton verticalSegmentButton() {
		return control(new ToggleButton(), MessageSource.get("Vertical.Segment"), true, true, CSS.mediumButton, CSS.verticalSegment);
	}

	public static ToggleButton trendLineButton() {
		return control(new ToggleButton(), MessageSource.get("Trend.Line"), true, true, CSS.mediumButton, CSS.trendLine);
	}

	public static ToggleButton trendRayButton() {
		return control(new ToggleButton(), MessageSource.get("Trend.Ray"), true, true, CSS.mediumButton, CSS.trendRay);
	}

	public static ToggleButton trendSegmentButton() {
		return control(new ToggleButton(), MessageSource.get("Trend.Segment"), true, true, CSS.mediumButton, CSS.trendSegment);
	}

	public static Region spacer() {
		final Region spacer = new Region();
		HBox.setHgrow(spacer, Priority.ALWAYS);
		VBox.setVgrow(spacer, Priority.ALWAYS);
		spacer.setMaxHeight(Double.MAX_VALUE);
		spacer.setMaxWidth(Double.MAX_VALUE);
		return spacer;
	}


}
